{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*tsfresh* returns a great number of features. Depending on the dynamics of the inspected time series, some of them maybe highly correlated. \n",
    "\n",
    "A common technique to deal with such highly correlated features are transformations such as a principal component analysis (PCA). This notebooks shows you how to perform a PCA on the extracted features."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "import pandas as pd\n",
    "\n",
    "\n",
    "class PCAForPandas(PCA):\n",
    "    \"\"\"This class is just a small wrapper around the PCA estimator of sklearn including normalization to make it \n",
    "    compatible with pandas DataFrames.\n",
    "    \"\"\"\n",
    "\n",
    "    def __init__(self, **kwargs):\n",
    "        self._z_scaler = StandardScaler()\n",
    "        super(self.__class__, self).__init__(**kwargs)\n",
    "\n",
    "        self._X_columns = None\n",
    "\n",
    "    def fit(self, X, y=None):\n",
    "        \"\"\"Normalize X and call the fit method of the base class with numpy arrays instead of pandas data frames.\"\"\"\n",
    "\n",
    "        X = self._prepare(X)\n",
    "\n",
    "        self._z_scaler.fit(X.values, y)\n",
    "        z_data = self._z_scaler.transform(X.values, y)\n",
    "\n",
    "        return super(self.__class__, self).fit(z_data, y)\n",
    "\n",
    "    def fit_transform(self, X, y=None):\n",
    "        \"\"\"Call the fit and the transform method of this class.\"\"\"\n",
    "\n",
    "        X = self._prepare(X)\n",
    "\n",
    "        self.fit(X, y)\n",
    "        return self.transform(X, y)\n",
    "\n",
    "    def transform(self, X, y=None):\n",
    "        \"\"\"Normalize X and call the transform method of the base class with numpy arrays instead of pandas data frames.\"\"\"\n",
    "\n",
    "        X = self._prepare(X)\n",
    "\n",
    "        z_data = self._z_scaler.transform(X.values, y)\n",
    "\n",
    "        transformed_ndarray = super(self.__class__, self).transform(z_data)\n",
    "\n",
    "        pandas_df = pd.DataFrame(transformed_ndarray)\n",
    "        pandas_df.columns = [\"pca_{}\".format(i) for i in range(len(pandas_df.columns))]\n",
    "\n",
    "        return pandas_df\n",
    "\n",
    "    def _prepare(self, X):\n",
    "        \"\"\"Check if the data is a pandas DataFrame and sorts the column names.\n",
    "\n",
    "        :raise AttributeError: if pandas is not a DataFrame or the columns of the new X is not compatible with the \n",
    "                               columns from the previous X data\n",
    "        \"\"\"\n",
    "        if not isinstance(X, pd.DataFrame):\n",
    "            raise AttributeError(\"X is not a pandas DataFrame\")\n",
    "\n",
    "        X.sort_index(axis=1, inplace=True)\n",
    "\n",
    "        if self._X_columns is not None:\n",
    "            if self._X_columns != list(X.columns):\n",
    "                raise AttributeError(\"The columns of the new X is not compatible with the columns from the previous X data\")\n",
    "        else:\n",
    "            self._X_columns = list(X.columns)\n",
    "\n",
    "        return X"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Load robot failure example\n",
    "\n",
    "Splits the data set in a train (1 <= id <= 87) and a test set (87 <= id <= 88). It is assumed that the selection process is done in the past (train) and features for future (test) data sets should be determined. The id 87 is overlapping so that the correctness of the procedure can be easily shown."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/mchrist/Documents/Research/tsfresh/venv/lib/python2.7/site-packages/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.\n",
      "  from pandas.core import datetools\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>time</th>\n",
       "      <th>F_x</th>\n",
       "      <th>F_y</th>\n",
       "      <th>F_z</th>\n",
       "      <th>T_x</th>\n",
       "      <th>T_y</th>\n",
       "      <th>T_z</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>63</td>\n",
       "      <td>-3</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>62</td>\n",
       "      <td>-3</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>61</td>\n",
       "      <td>-3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>63</td>\n",
       "      <td>-2</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>63</td>\n",
       "      <td>-3</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  time  F_x  F_y  F_z  T_x  T_y  T_z\n",
       "0   1     0   -1   -1   63   -3   -1    0\n",
       "1   1     1    0    0   62   -3   -1    0\n",
       "2   1     2   -1   -1   61   -3    0    0\n",
       "3   1     3   -1   -1   63   -2   -1    0\n",
       "4   1     4   -1   -1   63   -3   -1    0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tsfresh.examples.robot_execution_failures import download_robot_execution_failures, load_robot_execution_failures\n",
    "from tsfresh.feature_extraction import extract_features\n",
    "from tsfresh.feature_selection import select_features\n",
    "from tsfresh.utilities.dataframe_functions import impute\n",
    "from tsfresh.feature_extraction import ComprehensiveFCParameters, MinimalFCParameters, settings\n",
    "\n",
    "download_robot_execution_failures()\n",
    "df, y = load_robot_execution_failures()\n",
    "df_train = df.iloc[(df.id <= 87).values]\n",
    "y_train = y[0:-1]\n",
    "\n",
    "df_test = df.iloc[(df.id >= 87).values]\n",
    "y_test = y[-2:]\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Train"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Extract train features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Feature Extraction: 100%|██████████| 522/522 [00:00<00:00, 4771.52it/s]\n"
     ]
    }
   ],
   "source": [
    "X_train = extract_features(df_train, column_id='id', column_sort='time', default_fc_parameters=MinimalFCParameters(),\n",
    "                           impute_function=impute)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>variable</th>\n",
       "      <th>F_x__length</th>\n",
       "      <th>F_x__maximum</th>\n",
       "      <th>F_x__mean</th>\n",
       "      <th>F_x__median</th>\n",
       "      <th>F_x__minimum</th>\n",
       "      <th>F_x__standard_deviation</th>\n",
       "      <th>F_x__sum_values</th>\n",
       "      <th>F_x__variance</th>\n",
       "      <th>F_y__length</th>\n",
       "      <th>F_y__maximum</th>\n",
       "      <th>...</th>\n",
       "      <th>T_y__sum_values</th>\n",
       "      <th>T_y__variance</th>\n",
       "      <th>T_z__length</th>\n",
       "      <th>T_z__maximum</th>\n",
       "      <th>T_z__mean</th>\n",
       "      <th>T_z__median</th>\n",
       "      <th>T_z__minimum</th>\n",
       "      <th>T_z__standard_deviation</th>\n",
       "      <th>T_z__sum_values</th>\n",
       "      <th>T_z__variance</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.933333</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.249444</td>\n",
       "      <td>-14.0</td>\n",
       "      <td>0.062222</td>\n",
       "      <td>15.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>-10.0</td>\n",
       "      <td>0.222222</td>\n",
       "      <td>15.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.866667</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-3.0</td>\n",
       "      <td>0.956847</td>\n",
       "      <td>-13.0</td>\n",
       "      <td>0.915556</td>\n",
       "      <td>15.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>-20.0</td>\n",
       "      <td>4.222222</td>\n",
       "      <td>15.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.266667</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.442217</td>\n",
       "      <td>-4.0</td>\n",
       "      <td>0.195556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-0.666667</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.596285</td>\n",
       "      <td>-10.0</td>\n",
       "      <td>0.355556</td>\n",
       "      <td>15.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>-29.0</td>\n",
       "      <td>3.128889</td>\n",
       "      <td>15.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.266667</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.442217</td>\n",
       "      <td>-4.0</td>\n",
       "      <td>0.195556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-0.400000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>0.952190</td>\n",
       "      <td>-6.0</td>\n",
       "      <td>0.906667</td>\n",
       "      <td>15.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>-16.0</td>\n",
       "      <td>7.128889</td>\n",
       "      <td>15.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-0.333333</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.596285</td>\n",
       "      <td>-5.0</td>\n",
       "      <td>0.355556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>15.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>-0.600000</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>0.879394</td>\n",
       "      <td>-9.0</td>\n",
       "      <td>0.773333</td>\n",
       "      <td>15.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>-42.0</td>\n",
       "      <td>4.160000</td>\n",
       "      <td>15.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-0.133333</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.618241</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>0.382222</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 48 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "variable  F_x__length  F_x__maximum  F_x__mean  F_x__median  F_x__minimum  \\\n",
       "id                                                                          \n",
       "1                15.0           0.0  -0.933333         -1.0          -1.0   \n",
       "2                15.0           0.0  -0.866667         -1.0          -3.0   \n",
       "3                15.0           1.0  -0.666667         -1.0          -1.0   \n",
       "4                15.0           1.0  -0.400000          0.0          -2.0   \n",
       "5                15.0           2.0  -0.600000         -1.0          -2.0   \n",
       "\n",
       "variable  F_x__standard_deviation  F_x__sum_values  F_x__variance  \\\n",
       "id                                                                  \n",
       "1                        0.249444            -14.0       0.062222   \n",
       "2                        0.956847            -13.0       0.915556   \n",
       "3                        0.596285            -10.0       0.355556   \n",
       "4                        0.952190             -6.0       0.906667   \n",
       "5                        0.879394             -9.0       0.773333   \n",
       "\n",
       "variable  F_y__length  F_y__maximum      ...        T_y__sum_values  \\\n",
       "id                                       ...                          \n",
       "1                15.0           0.0      ...                  -10.0   \n",
       "2                15.0           3.0      ...                  -20.0   \n",
       "3                15.0           2.0      ...                  -29.0   \n",
       "4                15.0           5.0      ...                  -16.0   \n",
       "5                15.0           3.0      ...                  -42.0   \n",
       "\n",
       "variable  T_y__variance  T_z__length  T_z__maximum  T_z__mean  T_z__median  \\\n",
       "id                                                                           \n",
       "1              0.222222         15.0           0.0   0.000000          0.0   \n",
       "2              4.222222         15.0           0.0  -0.266667          0.0   \n",
       "3              3.128889         15.0           0.0  -0.266667          0.0   \n",
       "4              7.128889         15.0           1.0  -0.333333          0.0   \n",
       "5              4.160000         15.0           1.0  -0.133333          0.0   \n",
       "\n",
       "variable  T_z__minimum  T_z__standard_deviation  T_z__sum_values  \\\n",
       "id                                                                 \n",
       "1                  0.0                 0.000000              0.0   \n",
       "2                 -1.0                 0.442217             -4.0   \n",
       "3                 -1.0                 0.442217             -4.0   \n",
       "4                 -1.0                 0.596285             -5.0   \n",
       "5                 -1.0                 0.618241             -2.0   \n",
       "\n",
       "variable  T_z__variance  \n",
       "id                       \n",
       "1              0.000000  \n",
       "2              0.195556  \n",
       "3              0.195556  \n",
       "4              0.355556  \n",
       "5              0.382222  \n",
       "\n",
       "[5 rows x 48 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Select train features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:tsfresh.feature_selection.relevance:Infered classification as machine learning task\n",
      "WARNING:tsfresh.feature_selection.relevance:[test_feature_significance] Feature F_x__length is constant\n",
      "WARNING:tsfresh.feature_selection.relevance:[test_feature_significance] Feature F_y__length is constant\n",
      "WARNING:tsfresh.feature_selection.relevance:[test_feature_significance] Feature F_z__length is constant\n",
      "WARNING:tsfresh.feature_selection.relevance:[test_feature_significance] Feature T_x__length is constant\n",
      "WARNING:tsfresh.feature_selection.relevance:[test_feature_significance] Feature T_y__length is constant\n",
      "WARNING:tsfresh.feature_selection.relevance:[test_feature_significance] Feature T_z__length is constant\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>variable</th>\n",
       "      <th>T_y__variance</th>\n",
       "      <th>T_y__standard_deviation</th>\n",
       "      <th>F_z__standard_deviation</th>\n",
       "      <th>F_z__variance</th>\n",
       "      <th>F_x__standard_deviation</th>\n",
       "      <th>F_x__variance</th>\n",
       "      <th>T_x__variance</th>\n",
       "      <th>T_x__standard_deviation</th>\n",
       "      <th>F_y__variance</th>\n",
       "      <th>F_y__standard_deviation</th>\n",
       "      <th>...</th>\n",
       "      <th>F_z__sum_values</th>\n",
       "      <th>F_z__median</th>\n",
       "      <th>F_y__maximum</th>\n",
       "      <th>F_x__minimum</th>\n",
       "      <th>F_x__maximum</th>\n",
       "      <th>T_x__minimum</th>\n",
       "      <th>T_z__minimum</th>\n",
       "      <th>T_y__minimum</th>\n",
       "      <th>T_z__maximum</th>\n",
       "      <th>F_z__maximum</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>83</th>\n",
       "      <td>51.706667</td>\n",
       "      <td>7.190735</td>\n",
       "      <td>51.266450</td>\n",
       "      <td>2628.248889</td>\n",
       "      <td>5.329165</td>\n",
       "      <td>28.400000</td>\n",
       "      <td>1058.728889</td>\n",
       "      <td>32.538114</td>\n",
       "      <td>4.862222</td>\n",
       "      <td>2.205045</td>\n",
       "      <td>...</td>\n",
       "      <td>-1103.0</td>\n",
       "      <td>-53.0</td>\n",
       "      <td>-8.0</td>\n",
       "      <td>-28.0</td>\n",
       "      <td>-14.0</td>\n",
       "      <td>70.0</td>\n",
       "      <td>-16.0</td>\n",
       "      <td>-23.0</td>\n",
       "      <td>-10.0</td>\n",
       "      <td>-24.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>84</th>\n",
       "      <td>1563.528889</td>\n",
       "      <td>39.541483</td>\n",
       "      <td>291.988082</td>\n",
       "      <td>85257.040000</td>\n",
       "      <td>36.585729</td>\n",
       "      <td>1338.515556</td>\n",
       "      <td>6875.848889</td>\n",
       "      <td>82.920739</td>\n",
       "      <td>1143.555556</td>\n",
       "      <td>33.816498</td>\n",
       "      <td>...</td>\n",
       "      <td>-10671.0</td>\n",
       "      <td>-912.0</td>\n",
       "      <td>83.0</td>\n",
       "      <td>-110.0</td>\n",
       "      <td>-25.0</td>\n",
       "      <td>180.0</td>\n",
       "      <td>-28.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-208.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>85</th>\n",
       "      <td>14.755556</td>\n",
       "      <td>3.841296</td>\n",
       "      <td>14.501494</td>\n",
       "      <td>210.293333</td>\n",
       "      <td>4.616877</td>\n",
       "      <td>21.315556</td>\n",
       "      <td>40.995556</td>\n",
       "      <td>6.402777</td>\n",
       "      <td>8.088889</td>\n",
       "      <td>2.844097</td>\n",
       "      <td>...</td>\n",
       "      <td>423.0</td>\n",
       "      <td>32.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>-46.0</td>\n",
       "      <td>-7.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86</th>\n",
       "      <td>2788.595556</td>\n",
       "      <td>52.807154</td>\n",
       "      <td>121.420189</td>\n",
       "      <td>14742.862222</td>\n",
       "      <td>38.235179</td>\n",
       "      <td>1461.928889</td>\n",
       "      <td>202.426667</td>\n",
       "      <td>14.227673</td>\n",
       "      <td>257.315556</td>\n",
       "      <td>16.041058</td>\n",
       "      <td>...</td>\n",
       "      <td>-2216.0</td>\n",
       "      <td>-110.0</td>\n",
       "      <td>69.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>148.0</td>\n",
       "      <td>-95.0</td>\n",
       "      <td>-10.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>-14.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>87</th>\n",
       "      <td>6415.715556</td>\n",
       "      <td>80.098162</td>\n",
       "      <td>204.966621</td>\n",
       "      <td>42011.315556</td>\n",
       "      <td>57.753268</td>\n",
       "      <td>3335.440000</td>\n",
       "      <td>70.995556</td>\n",
       "      <td>8.425886</td>\n",
       "      <td>564.382222</td>\n",
       "      <td>23.756730</td>\n",
       "      <td>...</td>\n",
       "      <td>-14137.0</td>\n",
       "      <td>-1036.0</td>\n",
       "      <td>162.0</td>\n",
       "      <td>171.0</td>\n",
       "      <td>342.0</td>\n",
       "      <td>-142.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>222.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>-486.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "variable  T_y__variance  T_y__standard_deviation  F_z__standard_deviation  \\\n",
       "id                                                                          \n",
       "83            51.706667                 7.190735                51.266450   \n",
       "84          1563.528889                39.541483               291.988082   \n",
       "85            14.755556                 3.841296                14.501494   \n",
       "86          2788.595556                52.807154               121.420189   \n",
       "87          6415.715556                80.098162               204.966621   \n",
       "\n",
       "variable  F_z__variance  F_x__standard_deviation  F_x__variance  \\\n",
       "id                                                                \n",
       "83          2628.248889                 5.329165      28.400000   \n",
       "84         85257.040000                36.585729    1338.515556   \n",
       "85           210.293333                 4.616877      21.315556   \n",
       "86         14742.862222                38.235179    1461.928889   \n",
       "87         42011.315556                57.753268    3335.440000   \n",
       "\n",
       "variable  T_x__variance  T_x__standard_deviation  F_y__variance  \\\n",
       "id                                                                \n",
       "83          1058.728889                32.538114       4.862222   \n",
       "84          6875.848889                82.920739    1143.555556   \n",
       "85            40.995556                 6.402777       8.088889   \n",
       "86           202.426667                14.227673     257.315556   \n",
       "87            70.995556                 8.425886     564.382222   \n",
       "\n",
       "variable  F_y__standard_deviation      ...       F_z__sum_values  F_z__median  \\\n",
       "id                                     ...                                      \n",
       "83                       2.205045      ...               -1103.0        -53.0   \n",
       "84                      33.816498      ...              -10671.0       -912.0   \n",
       "85                       2.844097      ...                 423.0         32.0   \n",
       "86                      16.041058      ...               -2216.0       -110.0   \n",
       "87                      23.756730      ...              -14137.0      -1036.0   \n",
       "\n",
       "variable  F_y__maximum  F_x__minimum  F_x__maximum  T_x__minimum  \\\n",
       "id                                                                 \n",
       "83                -8.0         -28.0         -14.0          70.0   \n",
       "84                83.0        -110.0         -25.0         180.0   \n",
       "85                15.0           4.0          19.0         -46.0   \n",
       "86                69.0          21.0         148.0         -95.0   \n",
       "87               162.0         171.0         342.0        -142.0   \n",
       "\n",
       "variable  T_z__minimum  T_y__minimum  T_z__maximum  F_z__maximum  \n",
       "id                                                                \n",
       "83               -16.0         -23.0         -10.0         -24.0  \n",
       "84               -28.0          12.0           0.0        -208.0  \n",
       "85                -7.0          -1.0           0.0          50.0  \n",
       "86               -10.0          14.0           8.0         -14.0  \n",
       "87                13.0         222.0          44.0        -486.0  \n",
       "\n",
       "[5 rows x 24 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train_filtered = select_features(X_train, y_train)\n",
    "X_train_filtered.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Principal Component Analysis on train features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pca_0</th>\n",
       "      <th>pca_1</th>\n",
       "      <th>pca_2</th>\n",
       "      <th>pca_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>83</th>\n",
       "      <td>-1.828246</td>\n",
       "      <td>0.510962</td>\n",
       "      <td>0.070269</td>\n",
       "      <td>-0.102048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>84</th>\n",
       "      <td>3.741340</td>\n",
       "      <td>3.661448</td>\n",
       "      <td>1.263409</td>\n",
       "      <td>-0.115073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>85</th>\n",
       "      <td>-2.361277</td>\n",
       "      <td>-0.105445</td>\n",
       "      <td>-0.078477</td>\n",
       "      <td>0.292859</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86</th>\n",
       "      <td>0.261576</td>\n",
       "      <td>0.129725</td>\n",
       "      <td>1.586737</td>\n",
       "      <td>1.390926</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>87</th>\n",
       "      <td>4.337937</td>\n",
       "      <td>3.201585</td>\n",
       "      <td>1.248812</td>\n",
       "      <td>4.419234</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       pca_0     pca_1     pca_2     pca_3\n",
       "83 -1.828246  0.510962  0.070269 -0.102048\n",
       "84  3.741340  3.661448  1.263409 -0.115073\n",
       "85 -2.361277 -0.105445 -0.078477  0.292859\n",
       "86  0.261576  0.129725  1.586737  1.390926\n",
       "87  4.337937  3.201585  1.248812  4.419234"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca_train = PCAForPandas(n_components=4)\n",
    "X_train_pca = pca_train.fit_transform(X_train_filtered)\n",
    "\n",
    "# add index plus 1 to keep original index from robot example\n",
    "X_train_pca.index += 1\n",
    "\n",
    "X_train_pca.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Extract test features\n",
    "\n",
    "Only the selected features from the train data are extracted."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Feature Extraction: 100%|██████████| 12/12 [00:00<00:00, 2301.09it/s]\n"
     ]
    }
   ],
   "source": [
    "X_test_filtered = extract_features(df_test, column_id='id', column_sort='time',\n",
    "                                   kind_to_fc_parameters=settings.from_columns(X_train_filtered.columns),\n",
    "                                   impute_function=impute)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>variable</th>\n",
       "      <th>F_x__maximum</th>\n",
       "      <th>F_x__minimum</th>\n",
       "      <th>F_x__standard_deviation</th>\n",
       "      <th>F_x__variance</th>\n",
       "      <th>F_y__maximum</th>\n",
       "      <th>F_y__standard_deviation</th>\n",
       "      <th>F_y__variance</th>\n",
       "      <th>F_z__maximum</th>\n",
       "      <th>F_z__mean</th>\n",
       "      <th>F_z__median</th>\n",
       "      <th>...</th>\n",
       "      <th>T_x__minimum</th>\n",
       "      <th>T_x__standard_deviation</th>\n",
       "      <th>T_x__variance</th>\n",
       "      <th>T_y__minimum</th>\n",
       "      <th>T_y__standard_deviation</th>\n",
       "      <th>T_y__variance</th>\n",
       "      <th>T_z__maximum</th>\n",
       "      <th>T_z__minimum</th>\n",
       "      <th>T_z__standard_deviation</th>\n",
       "      <th>T_z__variance</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>87</th>\n",
       "      <td>342.0</td>\n",
       "      <td>171.0</td>\n",
       "      <td>57.753268</td>\n",
       "      <td>3335.440000</td>\n",
       "      <td>162.0</td>\n",
       "      <td>23.756730</td>\n",
       "      <td>564.382222</td>\n",
       "      <td>-486.0</td>\n",
       "      <td>-942.466667</td>\n",
       "      <td>-1036.0</td>\n",
       "      <td>...</td>\n",
       "      <td>-142.0</td>\n",
       "      <td>8.425886</td>\n",
       "      <td>70.995556</td>\n",
       "      <td>222.0</td>\n",
       "      <td>80.098162</td>\n",
       "      <td>6415.715556</td>\n",
       "      <td>44.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>9.903983</td>\n",
       "      <td>98.088889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>-6.0</td>\n",
       "      <td>-13.0</td>\n",
       "      <td>2.061283</td>\n",
       "      <td>4.248889</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.203698</td>\n",
       "      <td>1.448889</td>\n",
       "      <td>53.0</td>\n",
       "      <td>40.000000</td>\n",
       "      <td>42.0</td>\n",
       "      <td>...</td>\n",
       "      <td>-29.0</td>\n",
       "      <td>4.057366</td>\n",
       "      <td>16.462222</td>\n",
       "      <td>-27.0</td>\n",
       "      <td>2.628054</td>\n",
       "      <td>6.906667</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.884433</td>\n",
       "      <td>0.782222</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "variable  F_x__maximum  F_x__minimum  F_x__standard_deviation  F_x__variance  \\\n",
       "id                                                                             \n",
       "87               342.0         171.0                57.753268    3335.440000   \n",
       "88                -6.0         -13.0                 2.061283       4.248889   \n",
       "\n",
       "variable  F_y__maximum  F_y__standard_deviation  F_y__variance  F_z__maximum  \\\n",
       "id                                                                             \n",
       "87               162.0                23.756730     564.382222        -486.0   \n",
       "88                 5.0                 1.203698       1.448889          53.0   \n",
       "\n",
       "variable   F_z__mean  F_z__median      ...        T_x__minimum  \\\n",
       "id                                     ...                       \n",
       "87       -942.466667      -1036.0      ...              -142.0   \n",
       "88         40.000000         42.0      ...               -29.0   \n",
       "\n",
       "variable  T_x__standard_deviation  T_x__variance  T_y__minimum  \\\n",
       "id                                                               \n",
       "87                       8.425886      70.995556         222.0   \n",
       "88                       4.057366      16.462222         -27.0   \n",
       "\n",
       "variable  T_y__standard_deviation  T_y__variance  T_z__maximum  T_z__minimum  \\\n",
       "id                                                                             \n",
       "87                      80.098162    6415.715556          44.0          13.0   \n",
       "88                       2.628054       6.906667           6.0           3.0   \n",
       "\n",
       "variable  T_z__standard_deviation  T_z__variance  \n",
       "id                                                \n",
       "87                       9.903983      98.088889  \n",
       "88                       0.884433       0.782222  \n",
       "\n",
       "[2 rows x 24 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test_filtered"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Principal Component Analysis on test features\n",
    "\n",
    "The PCA components of the id 87 are the same as in the previous train PCA."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pca_0</th>\n",
       "      <th>pca_1</th>\n",
       "      <th>pca_2</th>\n",
       "      <th>pca_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>87</th>\n",
       "      <td>4.337937</td>\n",
       "      <td>3.201585</td>\n",
       "      <td>1.248812</td>\n",
       "      <td>4.419234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>-2.510650</td>\n",
       "      <td>-0.139986</td>\n",
       "      <td>-0.469103</td>\n",
       "      <td>0.243084</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       pca_0     pca_1     pca_2     pca_3\n",
       "87  4.337937  3.201585  1.248812  4.419234\n",
       "88 -2.510650 -0.139986 -0.469103  0.243084"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test_pca = pca_train.transform(X_test_filtered)\n",
    "\n",
    "# reset index to keep original index from robot example\n",
    "X_test_pca.index = [87, 88]\n",
    "\n",
    "X_test_pca"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
